package com.example.day3_notification;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class ExitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exit);
    }

    long lastTime = 0;//必须声明成员变量
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){//点击返回键
            if(System.currentTimeMillis() - lastTime >1000){//单击
                Toast.makeText(this, "再按一次退出app", Toast.LENGTH_SHORT).show();
                lastTime = System.currentTimeMillis();//记录这次的时间
                return false;//不关闭activity
            }else{//双击
                lastTime = System.currentTimeMillis();//记录这次的时间
                finish();

            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
